package com.alter.global.base;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

import com.alter.global.base.article.service.ArticleCenterService;
import com.alter.model.AfArticle;
import com.alter.model.AfTag;
import com.alter.model.SeNavBar;
import com.alter.model.SeUser;
import com.alter.system.service.SysUIService;

@Controller
@RequestMapping("/article")
public class ArticleCenterAction {
    // private Log logger =
    // LogFactory.getLog("com.alter.global.base.ArticleCenterAction");

    @Autowired
    private ArticleCenterService articleService;
    @Autowired
    private SysUIService uiService;

    /**
     * get 前往操作页面。
     * 
     * @return
     */
    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String toCreateActical(Model model) {
	AfArticle artile = new AfArticle();
	// 这里和struts一样 不需要定义绑定对象，但是 seam中的绑定是需要实例化绑定对象的。 坑
	// artile.setBar(new SeNavBar());
	// 这里可以用于设置一些 FORM 的初始化信息。
	model.addAttribute("article", artile);
	model.addAttribute("narList", uiService.getSysNavBarList());
	return "/manage/newactical";
    }

    /**
     * <p>
     * 	创建新文章<br> 肯跌啊, 只允许一个命令对象, 即之允许一个@modelAttribute 出现在参数列表中，多个虽然不会错误,但是会照常参数绑定混乱
     * 
     * @param article
     *            文章对象
     * @param context
     *            文章内容
     * @param tags
     *            文章标签
     * @param user
     *            当前系统登录用户。
     * @return
     */
    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String createActical(@ModelAttribute("article") AfArticle article, BindingResult result, SessionStatus status,
	    @RequestParam("fileContent") String context, @RequestParam("labels") String tags, WebRequest request) {
	SeUser user = (SeUser) request.getAttribute("user", RequestAttributes.SCOPE_SESSION);
	List<AfTag> tagslist = new ArrayList<AfTag>(5);
	if (StringUtils.isNotEmpty(tags)) {
	    String[] arr = tags.split(",");
	    for (int i = 0; i < arr.length; i++) {
		AfTag tag = new AfTag();
		tag.setName(arr[i].toLowerCase().trim());
		tagslist.add(tag);
	    }
	}
	article.setCreater(user.getName());
	articleService.createArticle(article, context, tagslist);
	return "redirect:/manage/article";
    }

    /**
     * 浏览文章 利用reset特性。
     * 
     * @param aid
     * @return
     */
    @RequestMapping(value = "/{aid}", method = RequestMethod.GET)
    public String viewActical(Model model, @PathVariable String aid) throws Exception {
	AfArticle art = articleService.findByAid(aid);
	model.addAttribute("article", art);
	// NAV
	// 没有向上查找的方法。
	// uiService.getsystemNavTree(null);
	List<SeNavBar> navbars = new ArrayList<SeNavBar>();
	navbars.add(art.getBar());
	model.addAttribute("navbars", navbars);
	// content
	StringBuffer sb = new StringBuffer();
	Reader reader = art.getContent().getCharacterStream();
	BufferedReader br = new BufferedReader(reader);
	String line = br.readLine();
	while (line != null) {
	    sb.append(line + "\n");
	    line = br.readLine();
	}
	model.addAttribute("content", sb.toString());
	return "/manage/viewartical";
    }
    
}
